# -*- coding: utf-8 -*-
"""
Created on Wed Nov 02 17:11:21 2016

@author: Divyashri Sreedharan Nair
"""

from androguard.core.bytecodes import apk 
import os
cwd = os.getcwd()

def grab_filename(apk) :
	"""
		@param apk : an APK instance
		@rtype : the APK's filename
	"""
	return apk.filename.split('/')[-1]

def grab_application_package_name(apk) :
	"""
		@param apk : an APK instance
		@rtype : the package name
	"""
	return apk.get_package()
 
def grab_application_permission_list(apk) :
	"""
		@param apk : an APK instance
		@rtype : the permission list
	"""
	return apk.get_permissions()

def output_image(permlist, f) :
    try:
        from PIL import Image
        from PIL import ImageDraw
    except:
        import Image
    
    i = Image.new("RGB",(500,len(permlist)*10))
    d = ImageDraw.Draw(i)
    
    for val,r in enumerate(permlist):
        d.text((0,val*10),r)
    i.save(open(f,"wb"),"PNG")
    
print("=====================================")
TEST = "C:/Users/admin/Desktop/samples/samples/ADRD/ffe62967b75aab56710110b26baa69acd47a81dd.apk"
OUTPUT = cwd+"/result.png"

a = apk.APK(TEST)
print "\nAPK Filename: \n"+ str(grab_filename(a))

output_image(grab_application_permission_list(a), OUTPUT);
print("=====================================")